//
//  TicketListCell.m
//  WeiShangKeProj
//
//  Created by 焱 孙 on 15/5/28.
//  Copyright (c) 2015年 visionet. All rights reserved.
//

#import "TicketListCell.h"
#import "UIViewExt.h"
#import "EquipmentVo.h"

@interface TicketListCell ()
{
    UILabel *lblServiceType;
    UILabel *lblEquipmentInfo;
    UILabel *lblHospital;
    
    UIImageView *imgViewWarning;
}

@end

@implementation TicketListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        // Initialization code
        self.lblStatus = [[UILabel alloc] initWithFrame:CGRectZero];
        self.lblStatus.font = [UIFont systemFontOfSize:13];
        self.lblStatus.lineBreakMode = NSLineBreakByWordWrapping;
        self.lblStatus.textColor = [UIColor whiteColor];
        self.lblStatus.textAlignment = NSTextAlignmentCenter;
        [self.lblStatus.layer setBorderWidth:0];
        [self.lblStatus.layer setCornerRadius:10];
        self.lblStatus.layer.borderColor = [[UIColor clearColor] CGColor];
        [self.lblStatus.layer setMasksToBounds:YES];
        [self.contentView addSubview:self.lblStatus];
        
        lblServiceType = [[UILabel alloc] initWithFrame:CGRectZero];
        lblServiceType.font = [UIFont systemFontOfSize:13];
        lblServiceType.lineBreakMode = NSLineBreakByWordWrapping;
        lblServiceType.textColor = [UIColor whiteColor];
        lblServiceType.backgroundColor = COLOR(51, 51, 102, 1.0);
        lblServiceType.textAlignment = NSTextAlignmentCenter;
        [lblServiceType.layer setBorderWidth:0];
        [lblServiceType.layer setCornerRadius:10];
        lblServiceType.layer.borderColor = [[UIColor clearColor] CGColor];
        [lblServiceType.layer setMasksToBounds:YES];
        [self.contentView addSubview:lblServiceType];
        
        self.lblTicketNum = [[UILabel alloc] initWithFrame:CGRectZero];
        self.lblTicketNum.backgroundColor = [UIColor clearColor];
        self.lblTicketNum.font = [UIFont systemFontOfSize:14];
        self.lblTicketNum.lineBreakMode = NSLineBreakByWordWrapping;
        self.lblTicketNum.textColor = COLOR(51,51,51,1);
        self.lblTicketNum.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:self.lblTicketNum];
        
        lblEquipmentInfo = [[UILabel alloc] initWithFrame:CGRectZero];
        lblEquipmentInfo.backgroundColor = [UIColor clearColor];
        lblEquipmentInfo.font = [UIFont systemFontOfSize:14];
        lblEquipmentInfo.lineBreakMode = NSLineBreakByWordWrapping;
        lblEquipmentInfo.textColor = COLOR(51,51,51,1);
        lblEquipmentInfo.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:lblEquipmentInfo];
        
        lblHospital = [[UILabel alloc] initWithFrame:CGRectZero];
        lblHospital.backgroundColor = [UIColor clearColor];
        lblHospital.font = [UIFont systemFontOfSize:14];
        lblHospital.lineBreakMode = NSLineBreakByWordWrapping;
        lblHospital.textColor = COLOR(149,149,149,1);
        lblHospital.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:lblHospital];
        
        self.imgViewDateTime = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_time"]];
        [self.contentView addSubview:self.imgViewDateTime];
        
        self.lblDateTime = [[UILabel alloc] initWithFrame:CGRectZero];
        self.lblDateTime.backgroundColor = [UIColor clearColor];
        self.lblDateTime.font = [UIFont systemFontOfSize:13];
        self.lblDateTime.lineBreakMode = NSLineBreakByWordWrapping;
        self.lblDateTime.textColor = COLOR(149,149,149,1);
        self.lblDateTime.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.lblDateTime];
        
        imgViewWarning = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ticket_warning"]];
        imgViewWarning.frame = CGRectMake(kScreenWidth-20-15, 15, 20, 20);
        imgViewWarning.hidden = YES;
        [self.contentView addSubview:imgViewWarning];
        
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)initWithData:(TicketVo*)ticketVo
{
    CGFloat fHeight = 13.5;
    //status
    self.lblStatus.backgroundColor = [BusinessCommon getTicketColorByStatus:ticketVo.strStatus];
    self.lblStatus.text = ticketVo.strStatusName;
    CGSize sizeLbl = [Common getStringSize:self.lblStatus.text font:self.lblStatus.font bound:CGSizeMake(MAXFLOAT, 20) lineBreakMode:self.lblStatus.lineBreakMode];
    self.lblStatus.frame = CGRectMake(15, fHeight, sizeLbl.width+10, 20);
    if (self.lblStatus.text.length > 0)
    {
        self.lblStatus.hidden = NO;
    }
    else
    {
        self.lblStatus.hidden = YES;
    }
    
    //维修单类型
    lblServiceType.text = ticketVo.strServiceTypeName;
    sizeLbl = [Common getStringSize:lblServiceType.text font:lblServiceType.font bound:CGSizeMake(MAXFLOAT, 20) lineBreakMode:lblServiceType.lineBreakMode];
    lblServiceType.frame = CGRectMake(self.lblStatus.right+7.5, fHeight, sizeLbl.width+10, 20);
    if (lblServiceType.text.length > 0)
    {
        lblServiceType.hidden = NO;
    }
    else
    {
        lblServiceType.hidden = YES;
    }
    
    //工单号
    self.lblTicketNum.text = ticketVo.strTicketNum;
    self.lblTicketNum.frame = CGRectMake(lblServiceType.right+10, fHeight+1, kScreenWidth - self.lblStatus.right-25, 18);
    fHeight += 18 + 7.5;
    
    //设备信息
    lblEquipmentInfo.text = [NSString stringWithFormat:@"%@ [%@]",ticketVo.equipmentVo.strTypeName,ticketVo.equipmentVo.strSeq];
    lblEquipmentInfo.frame = CGRectMake(15, fHeight, kScreenWidth-30, 18);
    fHeight += 18 + 5;
    
    //医院
    lblHospital.text = ticketVo.equipmentVo.strHospitalName;
    lblHospital.frame = CGRectMake(15, fHeight, kScreenWidth-152, 18);
    
    //date time
    self.lblDateTime.text = [Common getDateTimeStrStyle2:ticketVo.strDateTime andFormatStr:@"yyyy-MM-dd HH:mm"];
    self.lblDateTime.frame = CGRectMake(kScreenWidth-120, fHeight, 115, 17);
    
    self.imgViewDateTime.frame = CGRectMake(self.lblDateTime.left-12, fHeight+2.5, 12, 12);
    
    //设置警告背景(当状态为已分配并且2小时没有开始的维修单)
    if ([ticketVo.strStatus isEqualToString:@"A"] && [self checkMoreTwoHourFromNowTo:ticketVo.dateUpdate])
    {
        imgViewWarning.hidden = NO;
    }
    else
    {
        imgViewWarning.hidden = YES;
    }
}

- (BOOL)checkMoreTwoHourFromNowTo:(NSDate *)date
{
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    return (interval > 2*3600);
}

@end
